package yxy.game.pm2.active.command.actpay;

import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.chargepake;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.ActiveModule_每日礼包;
import yxy.game.pm2.active.module.subordinateclass.DataIdOrderId;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.PayMessage;
import yxy.game.pm2.message.PayMessage.DayPayInfoRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付活动 - 每日礼包   
 * @author Dell
 *
 */
@MessageType(proto = Command.DayPayInfo, classtype = PayMessage.DayPayInfoRequest.class)
public class CMD_Active_每日礼包_Info extends Command<DayPayInfoRequest> {
	
	private static final Logger logger = Logger.create(CMD_Active_每日礼包_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, DayPayInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();

		PayMessage.DayPayInfoResponse.Builder response = PayMessage.DayPayInfoResponse.newBuilder();
		
		try {
			handle_每日礼包(messageSource, response, pcode);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		send(messageSource, response, Result.DataError);
	}

	private void send(MessageSource messageSource, PayMessage.DayPayInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
	public void handle_每日礼包(MessageSource messageSource, PayMessage.DayPayInfoResponse.Builder response, String pcode) {
		ActiveModule_每日礼包 am = ActiveModule_每日礼包.get(pcode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		ArrayList<DataIdOrderId> finishs = am.getFinish();
		ArrayList<DataIdOrderId> locks = am.getLock();

		Map<String, chargepake> cpmap = DataCenter.getAllData(chargepake.class);
		for(chargepake cp : cpmap.values()) {
			PayMessage.ActiveDayPay.Builder build = PayMessage.ActiveDayPay.newBuilder();
			
//			JSONObject dataidjson = new JSONObject();
			int chargeid = cp.getChargeid();
			int limit = cp.getLimit();
			String desc = cp.getDescribe();
			int[] rewardtypes = cp.getRewardtype();
			int[] rewardids = cp.getRewardid();
			int[] rewardnums = cp.getRewardnum();
			String pakename = cp.getPakename();
			int[] shuangbeis = cp.getShuangbei();
			
			build.setGoodsid(cp.getChargeid());
			build.setPackname(pakename);
			build.setDesc(desc);
			build.setLimit(cp.getLimit());
			build.setRebate(cp.getRebate()); //返利倍数
	
			for(int i = 0; i < rewardtypes.length; i++) {
				PayMessage.ActiveDayPayReward.Builder dpr = PayMessage.ActiveDayPayReward.newBuilder();
				
				int rewardtype = rewardtypes[i];
				int rewardid = rewardids[i];
				int rewardnum = rewardnums[i];
				int shuangbei = shuangbeis[i];
				
				Currency cur = Currency.create(rewardtype, rewardid, rewardnum);
				if(cur == null) {
					continue;
				}
				
				PackageMessage.Item item = new CurrencyHandle().packItem(cur);
				dpr.setCur(item);
				dpr.setShaungbei(shuangbei);
				build.addPayreward(dpr.build());
			}
			
			int finish = 0;
			for(int i = 0; i < finishs.size(); i++) {
				DataIdOrderId finishdio = finishs.get(i);
				if(finishdio.getDataid() == chargeid) {
					HashMap<String, DateTime> odmap = finishdio.getMyorderinfoMap();
					if(odmap == null) {
						continue;
					}
					finish = odmap.size();
				}
			}
			
			int lock = 0;
			for (int i = 0; i < locks.size(); i++) {
				DataIdOrderId lockdio = locks.get(i);
				if(lockdio.getDataid() == chargeid) {
					HashMap<String, DateTime> odmap = lockdio.getMyorderinfoMap();
					if(odmap == null) {
						continue;
					}
					lock = odmap.size();
				}
			}
			
			int canbuynum = limit - finish;
			canbuynum = canbuynum < 0? 0:canbuynum;
			
			build.setCanbuynum(canbuynum);
			response.addAps(build.build());
		}
		
		response.setDaygift(am.cangetdaygift());
		response.setEnd(ActiveUtil.time0(am.getUpdatetime()).plusDays(1).getMillis());
		
		send(messageSource, response, Result.Success);
	}
	

	
}
